package cz.muni.fi.pa165.travelagency.persistence;

import cz.muni.fi.pa165.travelagency.entities.Customer;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Jan Novotný
 * @version 1.0
 * @created 11-10-2012
 */
@Stateless
public class CustomerDAO implements ICustomerDAO {
    private static final Logger logger = Logger.getLogger(CustomerDAO.class.getName());
    
    @PersistenceContext(unitName = "LivePU")
    private EntityManager em;

    void setEntityManager(EntityManager em) {
        this.em = em;
    }
    
    @Override
    public void createCustomer(Customer customer) {
        if (customer == null || 
            customer.getName() == null || customer.getAddress() == null || customer.getPhoneNumber() == null ||
            customer.getPhoneNumber().length() > 16) {
            throw new IllegalArgumentException();
        }
        
        if (customer.getId() != null) {
            throw new IllegalStateException();
        }
        
        try {
            em.persist(customer);
        } catch (Exception e) {
            logger.severe(e.getMessage());
            throw new DAOException(e.getMessage(), e);
        }
    }

    @Override
    public void deleteCustomer(Customer customer) {
        if (customer == null) {
            throw new IllegalArgumentException();
        }
        
        if (customer.getId() == null) {
            throw new IllegalStateException();
        }

        try {
            customer = em.merge(customer);
            em.remove(customer);
        } catch (Exception e) {
            logger.severe(e.getMessage());
            throw new DAOException(e.getMessage(), e);
        }        
    }

    @Override
    public List<Customer> findAll() {
        try {
            Query q = em.createQuery("SELECT customer FROM Customer customer");
            List<Customer> res = q.getResultList();
            return res;
        } catch (Exception e) {
            logger.severe(e.getMessage());
            throw new DAOException(e.getMessage(), e);
        }
    }

    @Override
    public List<Customer> findByName(String name) {
        if (name == null) {
            throw new IllegalArgumentException();
        }

        try {
            Query q = em.createQuery("SELECT customer FROM Customer customer WHERE name LIKE :name");
            q.setParameter("name", "%" + name + "%");
            List<Customer> res = q.getResultList();
            return res;
        } catch (Exception e) {
            logger.severe(e.getMessage());
            throw new DAOException(e.getMessage(), e);
        }        
    }

    @Override
    public Customer getCustomer(Long id) {
        if (id == null) {
            throw new IllegalArgumentException();
        }
        
        Customer c = null;
        try {
            c = em.find(Customer.class, id);
        } catch (Exception e) {
            logger.severe(e.getMessage());
            throw new DAOException(e.getMessage(), e);
        }     
        return c;
    }

    @Override
    public void updateCustomer(Customer customer) {
        if (customer == null || 
            customer.getName() == null || customer.getAddress() == null || customer.getPhoneNumber() == null ||
            customer.getPhoneNumber().length() > 16) {
            throw new IllegalArgumentException();
        }
        
        if (customer.getId() == null) {
            throw new IllegalStateException();
        }
        
        try {
            em.merge(customer);
        } catch (Exception e) {
            logger.severe(e.getMessage());
            throw new DAOException(e.getMessage(), e);
        }        
    }   

}
